package com.ncmmall.api.apis.mall;


import com.ncmmall.api.apis.service.Result;
import com.feijin.commons.lang.Requires;
import com.ncmmall.domain.business.news.News;
import com.ncmmall.domain.business.news.QNews;
import com.ncmmall.querychannel.service.QueryChannelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 公告
 *
 * @author likaikai
 * @da
 */
@RestController
@RequestMapping(value = "/news")
public class NewsApi {

    private Logger logger = LoggerFactory.getLogger(NewsApi.class);

    @Autowired
    private QueryChannelService queryChannelService;

    @Value("${store.domain}")
    private String storeDomain;

    /**
     * 公告详情
     *
     * @param id
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ResponseBody
    public Result detail(long id) {
        Result result = new Result();
        try {
            News news = queryChannelService.findOne(QNews.news.id.eq(id), News.class);
            Requires.notNull(news, "公告不存在");

            return result.ok().inject(news);

        } catch (IllegalArgumentException e) {
            return result.fail().setMsg(e.getMessage());
        }catch (Exception e) {

            logger.error("[公告详情获取数据异常]", e);
            return result.fail().setMsg("网络连接失败");
        }

    }


}
